/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.util.Arrays;
import main.WinkelApplication;
import user.UserHandler;

import java.awt.GraphicsEnvironment;
import java.awt.Point;


/**
 *
 * @author Wouter
 */
public class StartScreen extends javax.swing.JPanel {

	/**
	 * Creates new form MainMenu
	 */
	public StartScreen() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginErrorDialog = new javax.swing.JDialog();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        RegisterDialog = new javax.swing.JDialog();
        jLabel18 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        register_submitbutton = new javax.swing.JButton();
        register_password = new javax.swing.JPasswordField();
        register_password2 = new javax.swing.JPasswordField();
        register_backbutton = new javax.swing.JButton();
        register_firstname = new javax.swing.JTextField();
        register_lastname = new javax.swing.JTextField();
        register_username = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        registerErrorDialog = new javax.swing.JDialog();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        loginErrorDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        loginErrorDialog.setTitle("Oufit4you - Melding");
        loginErrorDialog.setAlwaysOnTop(true);
        loginErrorDialog.setResizable(false);
        loginErrorDialog.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel8.setText("Foutmelding:");
        loginErrorDialog.getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 227, -1));

        jLabel9.setText("Inloggegevens incorrect, probeer het opnieuw.");
        loginErrorDialog.getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/sluit_melding_button.png"))); // NOI18N
        jButton3.setToolTipText("Sluiten kreng");
        jButton3.setBorderPainted(false);
        jButton3.setContentAreaFilled(false);
        jButton3.setFocusPainted(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        loginErrorDialog.getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 70, 140, 40));

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/bg.png"))); // NOI18N
        jLabel10.setText("jLabel10");
        loginErrorDialog.getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 250, 120));

        RegisterDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        RegisterDialog.setTitle("outfit4you - Registreer account");
        RegisterDialog.setAlwaysOnTop(true);
        RegisterDialog.setLocationByPlatform(true);
        RegisterDialog.setModal(true);
        RegisterDialog.setResizable(false);
        RegisterDialog.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel18.setFont(new java.awt.Font("Segoe UI", 0, 24));
        jLabel18.setText("NIEUW ACCOUNT AANMAKEN");
        RegisterDialog.getContentPane().add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, 20));
        RegisterDialog.getContentPane().add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 330, 10));

        jLabel19.setText("Voornaam:");
        RegisterDialog.getContentPane().add(jLabel19, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, -1, -1));

        jLabel20.setText("Achternaam:");
        RegisterDialog.getContentPane().add(jLabel20, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, -1, -1));

        jLabel21.setText("Gebruikersnaam:");
        RegisterDialog.getContentPane().add(jLabel21, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

        jLabel22.setText("Wachtwoord:");
        RegisterDialog.getContentPane().add(jLabel22, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 150, -1, -1));

        jLabel23.setText("Wachtwoord herhalen:");
        RegisterDialog.getContentPane().add(jLabel23, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 180, -1, -1));

        register_submitbutton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/registreer_button2.png"))); // NOI18N
        register_submitbutton.setBorderPainted(false);
        register_submitbutton.setContentAreaFilled(false);
        register_submitbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                register_submitbuttonActionPerformed(evt);
            }
        });
        RegisterDialog.getContentPane().add(register_submitbutton, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 210, 140, 40));
        RegisterDialog.getContentPane().add(register_password, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 150, 190, -1));
        RegisterDialog.getContentPane().add(register_password2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 180, 190, -1));

        register_backbutton.setFont(new java.awt.Font("Tahoma", 1, 12));
        register_backbutton.setText("Ga terug");
        register_backbutton.setBorder(null);
        register_backbutton.setBorderPainted(false);
        register_backbutton.setContentAreaFilled(false);
        register_backbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                register_backbuttonActionPerformed(evt);
            }
        });
        RegisterDialog.getContentPane().add(register_backbutton, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 235, -1, -1));

        register_firstname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                register_firstnameActionPerformed(evt);
            }
        });
        RegisterDialog.getContentPane().add(register_firstname, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 60, 190, -1));
        RegisterDialog.getContentPane().add(register_lastname, new org.netbeans.lib.awtextra.AbsoluteConstraints(149, 90, 190, -1));
        RegisterDialog.getContentPane().add(register_username, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 120, 190, -1));

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/bg.png"))); // NOI18N
        jLabel11.setText("jLabel11");
        jLabel11.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        RegisterDialog.getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, -40, -1, -1));

        RegisterDialog.getAccessibleContext().setAccessibleParent(registerErrorDialog);

        registerErrorDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        registerErrorDialog.setTitle("registreren - foutmelding");
        registerErrorDialog.setAlwaysOnTop(true);
        registerErrorDialog.setModal(true);
        registerErrorDialog.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel12.setText("bericht1");
        registerErrorDialog.getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 230, -1));

        jLabel13.setText("bericht2");
        registerErrorDialog.getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

        jButton4.setText("ok");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        registerErrorDialog.getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 70, -1, -1));

        setAutoscrolls(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setEnabled(false);
        setMaximumSize(new java.awt.Dimension(530, 400));
        setOpaque(false);
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 30, -1, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/teamlogo11.png"))); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 20, 390, -1));

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(133, 140, 130, 30));

        jPasswordField1.setToolTipText("s");
        jPasswordField1.setPreferredSize(new java.awt.Dimension(83, 20));
        add(jPasswordField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(133, 180, 130, 30));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Gebruikersnaam");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 140, 120, 30));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Wachtwoord");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 180, 120, 30));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/login_button.png"))); // NOI18N
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 230, 140, 40));

        jSeparator1.setForeground(new java.awt.Color(204, 204, 204));
        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 140, -1, 130));

        jLabel5.setText("Nog geen account? Registreer");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 140, 210, 20));

        jLabel6.setText("je nu, het is gratis!");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 160, -1, -1));

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/registreer_button.png"))); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 230, 140, 40));

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/bg.png"))); // NOI18N
        add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 495, -1));
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
	char[] password = jPasswordField1.getPassword();
	if(UserHandler.loginAttempt(jTextField1.getText(), password))
	{
		WinkelApplication.getInstance().showPanel(new CategoryList());
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
		WinkelApplication.getInstance().mainWindow.setBounds(center.x - 1280 / 2, center.y - 720 / 2, 1280, 720);
	}
	else
	{
		loginErrorDialog.setVisible(true);
		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
		loginErrorDialog.setBounds(center.x - 250 / 2, center.y - 145 / 2,250,145);
	}
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

	loginErrorDialog.dispose();
}//GEN-LAST:event_jButton3ActionPerformed

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
	Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
	RegisterDialog.setBounds(center.x - 355 / 2, center.y - 285 / 2, 355, 285);
	//RegisterDialog.setSize(390, 295);
	RegisterDialog.setVisible(true);
}//GEN-LAST:event_jButton2ActionPerformed

private void register_submitbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_register_submitbuttonActionPerformed
	Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
	registerErrorDialog.setBounds(center.x - 250 / 2, center.y - 93 / 2, 286, 151);
	
	String firstname = register_firstname.getText();
	String lastname = register_lastname.getText();
	String username = register_username.getText();
	char[] password = register_password.getPassword();
	char[] password2 = register_password2.getPassword();
	
	if(firstname.length() > 0 && lastname.length() > 0 && username.length() > 0 && register_password.getPassword().length > 0 && register_password2.getPassword().length > 0)
	{
		if(WinkelApplication.getQueryManager().getUser("gebruikersnaam", register_username.getText()) == null)
		{
			if(Arrays.equals(password, password2))
			{
				System.out.println("Register user: " + register_username.getText());
				
				String password3 = UserHandler.hash.encrypt(password);
				int userID = WinkelApplication.getQueryManager().addUser(firstname, lastname, username, password3);
				
				System.out.println(userID);
				
				if(userID != 0)
				{
					jLabel12.setText("Je account is succesvol aangemaakt! Je kunt nu");
					jLabel13.setText("met je gegevens inloggen!");
				}
				else
				{
					jLabel12.setText("Er is mis gegaan!");
					jLabel13.setText("Ooh nooo");
				}
			}
			else
			{
				jLabel12.setText("         De wachtwoorden komen niet overeen.");
				jLabel13.setText("");
			}
		}
		else
		{
			jLabel12.setText("           Deze gebruikersnaam is al bezet.");
			jLabel13.setText("");
		}
	}
	else
	{
		jLabel12.setText("Niet alle gegevens zijn goed ingevuld! Corrigeer");
		jLabel13.setText("dit en klik daarna weer op verzenden.");
	}

	registerErrorDialog.setVisible(true);
}//GEN-LAST:event_register_submitbuttonActionPerformed

private void register_backbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_register_backbuttonActionPerformed
	RegisterDialog.dispose();
}//GEN-LAST:event_register_backbuttonActionPerformed

private void register_firstnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_register_firstnameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_register_firstnameActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
	
	if(jLabel13.getText().equals("met je gegevens inloggen!"))
	{
		registerErrorDialog.dispose();
		RegisterDialog.dispose();
	}
	else
	{
		registerErrorDialog.dispose();
	}
}//GEN-LAST:event_jButton4ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog RegisterDialog;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JDialog loginErrorDialog;
    private javax.swing.JDialog registerErrorDialog;
    private javax.swing.JButton register_backbutton;
    private javax.swing.JTextField register_firstname;
    private javax.swing.JTextField register_lastname;
    private javax.swing.JPasswordField register_password;
    private javax.swing.JPasswordField register_password2;
    private javax.swing.JButton register_submitbutton;
    private javax.swing.JTextField register_username;
    // End of variables declaration//GEN-END:variables
}